package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.dao.TrainingsDao;
import com.orhon.smartcampus.modules.teacher.entity.Trainings;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLTrainingsInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.TrainingsInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddTrainingsMutation implements DataFetcher {

    @Autowired
    TrainingsDao trainingsDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加培训信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLTrainingsInput input = TrainingsInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Trainings trainings = null;

        if (input.getId() != null) {
            trainings = trainingsDao.get(Trainings.class, input.getId());
            trainings.setUpdatedAt(new Date());
        } else {
            trainings = new Trainings();
            trainings.setCreatedAt(new Date());
        }
        if(schoolId != null){

            trainings.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){
            trainings.setTeacher_id(input.getTeacher_id());
        }
        if(input.getName() != null){

            trainings.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getOrganizer() != null){

            trainings.setOrganizer(JSON.toJSONString(input.getOrganizer()));
        }
        if(input.getDuration() != null){

            trainings.setDuration(JSON.toJSONString(input.getDuration()));
        }
        if(input.getContent() != null){

            trainings.setContent(JSON.toJSONString(input.getContent()));
        }
        if(input.getStartdate() != null){

            trainings.setStartdate(input.getStartdate());
        }
        if(input.getEnddate() != null){

            trainings.setEnddate(input.getEnddate());
        }
        if(input.getImage() != null){

            trainings.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            trainings.setShenhe(input.getShenhe());
        }
        try{
            trainingsDao.saveOrUpdate(trainings);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

